<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="renderer" content="webkit">

    <title>新闻添加</title>

    <meta name="keywords" content="">
    <meta name="description" content="">

    <!--[if lt IE 8]>
    <script>
        alert('H+已不支持IE6-8，请使用谷歌、火狐等浏览器\n或360、QQ等国产浏览器的极速模式浏览本页面！');
    </script>
    <![endif]-->

    <link href="/static/admin/public/css/bootstrap.min.css?v=3.4.0" rel="stylesheet">
    <link href="/static/admin/public/css/font-awesome.min.css?v=4.3.0" rel="stylesheet">
    <link href="/static/admin/public/css/animate.min.css" rel="stylesheet">
    <link href="/static/toastr/toastr.min.css" rel="stylesheet"></link>
    <link href="/static/admin/public/css/style.min.css?v=3.2.0" rel="stylesheet">
    <link href="/static/admin/plugins/awesome-bootstrap-checkbox/awesome-bootstrap-checkbox.css?v=3.2.0" rel="stylesheet">
</head>

<body class="fixed-sidebar full-height-layout gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="ibox float-e-margins" >
        <div class="ibox-title">
            <h5>添加</h5>
        </div>
        <div class="ibox-content">
            <form id="commentForm" class="form-horizontal m-t">
                <div class="form-group"><label class="col-sm-2 control-label">新闻名称：</label>
                    <div class="col-sm-10"><input v-model="title" name="title" minlength="2" type="text" required="required" class="form-control"></div>
                </div>
                <div class="form-group"><label class="col-sm-2 control-label">新闻简介：</label>
                    <div class="col-sm-10">
                        <input v-model="summary" name="summary" minlength="2" type="text" required="required" class="form-control">
                    </div>
                </div>
<!--                <div class="form-group"><label class="col-sm-3 control-label">是否开启：</label>-->
<!--                    <div class="radio  radio-inline radio-info"><input type="radio" id="inlineRadio1" value="option1" name="radioInline" checked="checked"> <label for="inlineRadio1"> 是 </label></div>-->
<!--                    <div class="radio radio-inline"><input type="radio" id="inlineRadio2" value="option2" name="radioInline"> <label for="inlineRadio2"> 否 </label></div>-->
<!--                </div>-->
<!--                <input ref="inputer" type="file" name="upfile" id="upfile" @change="changepic()" class="upfile" accept="image/png, image/jpeg, image/gif, image/jpg" />-->
<!--                <label style="display: inline-block" for="upfile" class="upFileBtn">点击上传</label>-->
                <div class="form-group"><label class="col-sm-2 control-label">图片上传：</label>
                    <div class="col-sm-10">
                        <div class="input-group">
                            <input style="display: none" @change="changepic()" ref="inputer"  name="imageUrl" id="imageHidden" name="imageHidden" type="file" accept="image/png, image/jpeg, image/gif, image/jpg" class="form-control">
                            <input v-model="imageUrl" name="imageUrl" id="imageUrl" minlength="2" type="text" required="required" class="form-control">
                            <span class="input-group-btn">
                                <label v-if="imageUrl!=''" type="button" class="btn btn-success" @click="removeImage()">删除</label>
                                <label v-if="imageUrl==''" type="button" class="btn btn-primary" for="imageHidden">上传</label>
                            </span>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">新闻内容：</label>
                    <div class="col-sm-10">
                        <script id="container" name="content" type="text/plain">这里写你的初始化内容</script>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-4 col-sm-offset-3"><button type="button" @click="addContent()" class="btn btn-primary">提交</button></div>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- 全局js -->
<script src="/static/admin/public/js/jquery-2.1.1.min.js"></script>
<script src="/static/admin/public/js/bootstrap.min.js?v=3.4.0"></script>
<script src="/static/admin/plugins/metisMenu/jquery.metisMenu.js"></script>
<script src="/static/admin/plugins/slimscroll/jquery.slimscroll.min.js"></script>
<script src="/static/admin/plugins/layer/layer.min.js"></script>
<script type="text/javascript" src="/static/vue/vue.js"></script>

<!-- 自定义js -->
<script type="text/javascript" src="/static/util/axios.min.js"></script>
<script type="text/javascript" src="/static/admin/api/require.js"></script>
<script type="text/javascript" src="/static/admin/api/publicAjax.js"></script>

<!-- 第三方插件 -->

<script src="/static/toastr/toastr.min.js"></script>
<script src="/static/toastr/customToastr.js"></script>

<script src="/static/admin/plugins/validate/jquery.validate.min.js"></script>
<script src="/static/admin/plugins/validate/messages_zh.min.js"></script>


<!--编译器-->
<!-- 配置文件 -->
<script type="text/javascript" src="/static/admin/plugins/ueditor/ueditor.config.js"></script>
<!-- 编辑器源码文件 -->
<script type="text/javascript" src="/static/admin/plugins/ueditor/ueditor.all.js"></script>
<script>
    let ue = UE.getEditor('container',{
        autoHeight: false,
        initialFrameHeight: 300
    });
    var vmAddChannel = new Vue({
        el:"#commentForm",
        data:{
            title:'',
            content:'',
            summary:'',
            imageUrl:''
        },
        methods:{
            changepic:function(){
                var el = this;
                var inputDOM = el.$refs.inputer;
                var files = inputDOM.files;
                if(files.length<1){
                    $.customToastr('error','未选择图片')
                    return
                }
                var formData = new FormData();
                formData.append('files',files[0]);
                AjaxPost(formData,'/admin/image/upload').then((response)=>{
                    if(response.status==1){
                        $.customToastr('success',response.message)
                        this.imageUrl = response.result
                    }
                    console.log(response);
                })
            },
            removeImage:function(){
                var el = this;
                var postData = {
                    'file':el.imageUrl
                }
                if(el.imageUrl==''){
                    $.customToastr('warning','未上传无法删除')
                    return;
                }
                AjaxPost(postData,'/admin/image/remove').then((response)=>{
                    if(response.status==1){
                        $.customToastr('success',response.message)
                        this.imageUrl = ''
                    }
                    console.log(response);
                })


            },
            addContent:function () {
                let el = this;
                let content = ue.getContent()
                if(content === ""){
                    $.customToastr('error',"新闻的内容不能为空")
                    return
                }
                if($("#commentForm").valid()){
                    let postData = {
                        'title':el.title,
                        'content':content,
                        'summary':el.summary,
                        'image':el.imageUrl
                    }
                    AjaxPost(postData,'/admin/news/addNews').then((response=>{
                        console.log(response);
                        if(response.status==1){
                            $.customToastr('success',response.message)
                            setTimeout(()=>{
                                window.location.href='/admin/news/index'
                            },1000)
                        }
                    }))
                }
            }
        },
        mounted(){
        }
    })
</script>

</body>

</html>